package org.jash.sportnews2109.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.KeyEvent
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import org.jash.common.adapter.CommonAdapter
import org.jash.common.adapter.CommonViewHolder
import org.jash.common.logging
import org.jash.common.mvvm.BaseActivity
import org.jash.common.token
import org.jash.sportnews2109.App
import org.jash.sportnews2109.BR
import org.jash.sportnews2109.R
import org.jash.sportnews2109.databinding.ActivityDetailBinding
import org.jash.sportnews2109.databinding.CommentItemBinding
import org.jash.sportnews2109.entity.Comment
import org.jash.sportnews2109.vm.DetailViewModel
import java.util.Date

@Route(path = "/news/detail")
@AndroidEntryPoint
class DetailActivity : BaseActivity<ActivityDetailBinding, DetailViewModel>() {
    @Autowired
    @JvmField
    var id:Int = 0
    val replays = CommentFragment()
    val comment by lazy { Comment("", Date(), 0, id, 0, listOf(), 0) }
    val adapter by lazy { object : CommonAdapter<Comment>(R.layout.comment_item, BR.comment) {
        override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
            super.onBindViewHolder(holder, position)
            val itemBinding = holder.binding as CommentItemBinding
            itemBinding.showAll.setOnClickListener {
                replays.comment = data[position]
                replays.show(supportFragmentManager, "replays")
            }
            itemBinding.replay.setOnClickListener {
                comment.parent = data[position]
                binding.sendComment.requestFocus()
            }

        }
    } }
    override fun initView() {
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        binding.isLogin = token != null
        binding.comment = comment
        binding.collect.setOnClickListener {
            viewModel.collect(id)
        }
        binding.commentList.adapter = adapter
        binding.sendComment.setOnEditorActionListener { _, _, event ->
            when(event.action) {
                KeyEvent.ACTION_DOWN -> {
                    if (comment.content.isBlank()) {
                        comment.parent = null
                    } else {
                        viewModel.sendComment(comment)
                    }
                }
            }
            true
        }
    }

    override fun initData() {
        ARouter.getInstance().inject(this)
        viewModel.newsLiveData.observe(this) {
            binding.news = it
        }
        viewModel.collectLiveData.observe(this, this::collected)
        viewModel.commentsLiveData.observe(this, this::loadComment)
        viewModel.sendCommentLiveData.observe(this, this::sendComment)
        viewModel.loadNews(id)
        if (token != null) {
            viewModel.loadComments(id)
        }
    }
    fun collected(msg:String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        binding.news?.let{ it.collects += if (binding.collect.isChecked)  1 else -1}
//        binding.collect.text = "" + binding.news?.collects
    }
    fun loadComment(list:List<Comment>) {
        adapter += list
    }
    fun sendComment(msg:String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
        adapter.clear()
        viewModel.loadComments(id)
        binding.sendComment.setText("")
        comment.parent = null
    }
}